function Asteroid()
{
    var choice = Math.round( getRandomArbitary( 1, 100 ) );
    this.image = getAsteroidImage( choice );
    this.asteroidWidth = getAsteroidWidthOrHeight( choice );
    this.asteroidHeight = getAsteroidWidthOrHeight( choice );
    this.hp = getAsteroidHp( choice );
    this.speed = getAsteroidSpeed( choice );
    this.x = 0;
    this.y = 0;
                                               
    this.life = 'false';                                  
}                                       

Asteroid.prototype.draw = function( context )
{     
    context.save();
    
    context.translate( this.x, this.y );
    context.drawImage( this.image, 0, 0, this.asteroidWidth, this.asteroidHeight );

    context.restore();
};

function getRandomAsteroid( asteroids, i, numAsteroids, width, height, asteroidProperties )
{
    var chanceEmergenceAsteroids = 'false'; 
    var j = 0;
    var belongs = 'false';

    while ( chanceEmergenceAsteroids == 'false' )
    {   
        asteroidProperties.x = getRandomArbitary( 0, width - asteroidProperties.asteroidWidth );
        asteroidProperties.y = -1 * getRandomArbitary( asteroidProperties.asteroidHeight, height + asteroidProperties.asteroidHeight );
        for ( j = 0; j < numAsteroids; j++ )
        {                                                         
            belongs = intersectionSquares(
                          asteroidProperties.x,
                          asteroidProperties.y,
                          asteroidProperties.asteroidWidth,
                          asteroidProperties.asteroidHeight,
                          asteroids[j].x,
                          asteroids[j].y,
                          asteroids[j].asteroidWidth,
                          asteroids[j].asteroidHeight
                      );
            if ( belongs == 'true' )
            {
                break;
            }
        }
        if ( j == numAsteroids )
        {
            chanceEmergenceAsteroids = 'true';    
        }
    }
    return asteroidProperties;
}

function makeAsteroid( asteroids, i, numAsteroids, width, height )
{
    var newAsteroid = {x: 0, y: 0, life: 'false'};
    var asteroid = new Asteroid();                                                                                                                                                                                                             

    newAsteroid = getRandomAsteroid( asteroids, i, numAsteroids, width, height, asteroid );                                                                  
    asteroid.x = newAsteroid.x;
    asteroid.y = newAsteroid.y;
    asteroid.life = 'true';
    return asteroid; 
}
                     
function getAsteroids( numAsteroids, asteroids, width, height )
{
    var i = 0;                         

    if ( asteroids.length == 0 )
    {   
        for ( i = 0; i < numAsteroids; i++ )
        {                              
            asteroids[i] = makeAsteroid( asteroids, i, i, width, height );                                                            
        }
    }
    for ( i = 0; i < numAsteroids; i++ )
    {
        asteroids[i].y += asteroids[i].speed;
        if ( asteroids[i].y > height )
        {                                                                
            asteroids[i].life = 'false';             
        }
        if ( asteroids[i].life == 'false' )
        {   
            asteroids[i] = makeAsteroid( asteroids, i, numAsteroids, width, height );     
        }
    }
    return asteroids;
}

function makeAsteroidBangEffect( asteroid, context )
{                                         
    var image = document.getElementById( 'bang' );
    context.drawImage( image, asteroid.x, asteroid.y, asteroid.asteroidWidth, asteroid.asteroidHeight );    
}

function getAsteroidImage( choice )
{
    var image = 0;                
                                                                         
    if ( choice <= 25 )
    {
        image = document.getElementById( 'asteroid_1' ); 
    }
    if ( choice > 25 && choice <= 50 )
    {
        image = document.getElementById( 'asteroid_2' );    
    }
    if ( choice > 50 && choice <= 75 )
    {
        image = document.getElementById( 'asteroid_3' );    
    }
    if ( choice > 75 && choice <= 100 )
    {
        image = document.getElementById( 'asteroid_4' );    
    }
    return image;
}

function getAsteroidWidthOrHeight( choice )
{
    var asteroidWidth = 0;
                                                                                         
    if ( choice <= 25 )
    {
        asteroidWidth = 100;
    }
    if ( choice > 25 && choice <= 50 )
    {
        asteroidWidth = 90;
    }
    if ( choice > 50 && choice <= 75 )
    {
        asteroidWidth = 50;
    }
    if ( choice > 75 && choice <= 100 )
    {
        asteroidWidth = 50;
    }
    return asteroidWidth;
}
                                 
function getAsteroidHp( choice )
{
    var hp = 0;        
                                                                                 
    if ( choice <= 25 )
    {
        hp = 100;     
    }
    if ( choice > 25 && choice <= 50 )
    {
        hp = 50;
    }
    if ( choice > 50 && choice <= 75 )
    {
        hp = 25;
    }
    if ( choice > 75 && choice <= 100 )
    {
        hp = 25;
    }
    return hp;
}

function getAsteroidSpeed( choice )
{
    var speed = 0;      
                                                                                   
    if ( choice <= 25 )
    {                     
        speed = 2;
    }
    if ( choice > 25 && choice <= 50 )
    {
        speed = 2;
    }
    if ( choice > 50 && choice <= 75 )
    {
        speed = 4;
    }
    if ( choice > 75 && choice <= 100 )
    {
        speed = 4;
    }
    return speed;
}